﻿using System;
using Controller;
using Model;

public partial class ACPanel_NodeMedia_AddEditVideo : System.Web.UI.Page
{
    public static string _id;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDatatoDDL.BindSubLoaiChuyenMuc(ddlSubloaichuyenmuc);
            BindDatatoDDL.BindSubLoaiTourAll(ddlSubloaitour);
            BindDatatoDDL.BindDiaDiem(ddlDiadiem);
            var id = Request.QueryString["Id"];
            if (id != null)
            {
                // Thi goi BindData de cap nhat du lieu
                _id = id;
                BindData(_id);
            }            
        }
    }

    public void BindData(string id)
    {
        var controller = new VYC23Controller();
        var list = controller.GetById(id);
        if (list.Count > 0)
        {
            tbTen.Text = list[0].TieuDe;
            ddlSubloaichuyenmuc.SelectedValue = list[0].IdSubLoaiChuyenMuc.ToString();
            ddlSubloaitour.SelectedValue = list[0].IdSubLoaiTour.ToString();
            ddlDiadiem.SelectedValue = list[0].IdDiadiem.ToString();
            tbTomtat.Text = list[0].TomTat;
            tbLienket.Text = list[0].LienKet;
            cbTrangThai.Checked = Convert.ToBoolean(list[0].TrangThai);                       
        }
        else
        {
            // Du lieu khong ton tai
            litResult.Text = "<p class=\"alert alert-danger\">Lỗi! Dữ liệu không tồn tại..</p>";
        }
    }

    protected void btnThemvao_OnClick(object sender, EventArgs e)
    {
        try
        {
            var controller = new VYC23Controller();
            var model = new ModelVYC23();

            model.TieuDe = tbTen.Text;
            model.TomTat = tbTomtat.Text;
            model.LienKet = tbLienket.Text;
            model.TrangThai = Convert.ToInt32(cbTrangThai.Checked);

            model.IdSubLoaiChuyenMuc = Convert.ToInt32(ddlSubloaichuyenmuc.SelectedValue);
            model.IdSubLoaiTour = Convert.ToInt32(ddlSubloaitour.SelectedValue);
            model.IdDiadiem = Convert.ToInt32(ddlDiadiem.SelectedValue);
            var isSuccess = 0;
            if (!string.IsNullOrEmpty(_id))
            {
                model.Id = _id;
                isSuccess = controller.EditData(model);
            }
            else
            {
                model.Id = AppCommon.Common.ConvertToUnSign(tbTen.Text);
                isSuccess = controller.AddData(model);
            }
            if (isSuccess == 0)
            {
                // Chua thanh cong
                litResult.Text = "<p class=\"alert alert-danger\">Lỗi! Thêm dữ liệu thất bại.</p>";
            }
            else
            {
                litResult.Text = "<p class=\"alert alert-success\">Thêm dữ liệu thành công.</p>";
                ResetInput();
            }
        }
        catch (Exception)
        {
            litResult.Text = "<p class=\"alert alert-danger\">Lỗi! Thêm dữ liệu thất bại.</p>";
            throw;
        }
    }

    public void ResetInput()
    {
        tbTen.Text = "";
        cbTrangThai.Checked = false;
        _id = String.Empty;
    }


}